package com.probiz.estore.product.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductStat;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductStatDao;
import com.probiz.estore.product.service.ProductStatManager;


/**
 * Manager implementation for ProductStat, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductStatManagerImpl extends GenericManagerImpl<ProductStat> implements ProductStatManager {

	private ProductStatDao productStatDao = null;

	/**
	 * @param productStatDao
	 *            the productStatDao to set
	 */
	public void setProductStatDao(ProductStatDao productStatDao) {
		this.productStatDao = productStatDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productStatDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductStat entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductStat entity) {

	}

	public void doInitProductStatByCreateProduct(Product product) {
		/*if(product.getProductStat()==null){
			ProductStat productStat=new ProductStat();
			productStat.setBuyCount(0);
			productStat.setAverageRate(new BigDecimal(0));
			productStat.setReviewCount(0);
			productStat.setProduct(product);
			save(productStat);
		}*/
	}

	public ProductStat getProductStatByProduct(Integer productId) {
		return dao.getUniqueByProperty("product.productId", productId);
	}
	
	private ProductStat getProductStatForUpdateByProduct(Integer productId) {
		return dao.loadForUpdate(getProductStatByProduct(productId).getId());
	}
	
	public void doAdjustProductReviewRate(Integer productId,Integer rateValue,Short Operation){
		/*ProductStat productStat=getProductStatForUpdateByProduct(productId);
		Integer reviewCount=productStat.getReviewCount();
		BigDecimal total=productStat.getAverageRate().multiply(new BigDecimal(reviewCount));
		if(Operation.intValue()==1){
			total=total.add(new BigDecimal(rateValue));
			reviewCount++;
		}else{
			total=total.subtract(new BigDecimal(rateValue));
			reviewCount--;
		}
		if(reviewCount>0){
			BigDecimal averageRate=total.divide(new BigDecimal(reviewCount).setScale(3), 1,RoundingMode.HALF_UP);
			productStat.setAverageRate(averageRate);
			productStat.setReviewCount(reviewCount);
		}else{
			productStat.setAverageRate(new BigDecimal(0));
			productStat.setReviewCount(0);
		}
		save(productStat);*/
	}

	public void doAddProductBuyCount(Integer productId, Integer quantity) {
		if(logger.isInfoEnabled()){
			logger.info("产品:"+productId+",销售数量增加："+quantity);
		}
		ProductStat productStat=getProductStatForUpdateByProduct(productId);
		productStat.setBuyCount(productStat.getBuyCount()+quantity);
		dao.save(productStat);
	}

	public void doSubtractProductBuyCount(Integer productId, Integer quantity) {
		if(logger.isInfoEnabled()){
			logger.info("产品:"+productId+",销售数量减少："+quantity);
		}
		ProductStat productStat=getProductStatForUpdateByProduct(productId);
		productStat.setBuyCount(productStat.getBuyCount()-quantity);
		dao.save(productStat);
	}

}
